<?php

namespace Application\Service;
     
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use WebinoImageThumb\Module as WebinoImageThumb;

class FileService
{
    protected $rootPath = "./public/img/galerie/";
    protected $filePath = "./public/img/galerie/";
    protected $importPath = "./public/tmp/";

    protected $width_logo = 150;
    protected $height_logo = 43;

    protected $width_accueil = 447;
    protected $height_accueil = 250;

    protected $width_galerie = 709;
    protected $height_galerie = 460;

    protected $width_galerie_thumb = 70;
    protected $height_galerie_thumb = 70;

    public function __construct(WebinoImageThumb $thumbnailer)
    {
        $this->thumbnailer = $thumbnailer;
    }

    /**/
    public function setRootPath( $id = null, $type = "sejour" ) {
        if( $id  != null || $id != "" ){
            $this->filePath = $this->rootPath . $type . "__".$id."__/";
        }else {
            $this->filePath = $this->rootPath;
        }

        $this->creerDossierSiNonPresent();
    }

    public function setImportPath() {
        $this->filePath = $this->importPath;
        
        $this->creerDossierSiNonPresent();
    }

    public function creerDossierSiNonPresent() {
        if ( !is_dir( $this->filePath ) ) 
        {
            mkdir( $this->filePath );
        }
    }

    public function getRootPath()
    {
        return $this->filePath;
    }

    public function supprimeImageFileSystem ( $nomImageASupprimer ) {
        $filename = $this->filePath . $nomImageASupprimer;

        if( !empty( $nomImageASupprimer ) && !is_null( $nomImageASupprimer ) && file_exists($filename) ) {
            unlink( $filename );
        }

        if( $this->is_dir_empty () ){
            rmdir( $this->filePath );
        }
    }

    /* Est ce que mon dossier est vide */
    public function is_dir_empty() {
        if ( !is_readable( $this->filePath ) ) return NULL;

        $handle = opendir( $this->filePath );
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                return FALSE;
            }
        }
        return TRUE;
    }

    public function retirer_extension (  $filename ) {
        return preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
    }

    public function extraire_extension ( $filename ) {
        return pathinfo($filename, PATHINFO_EXTENSION);
    }

    public function retourne_nom_image_min ( $filename, $suffixe = "-min" ) {
        $extension = $this->extraire_extension( $filename );
        $nouveau_nom = $this->retirer_extension( $filename );
        $nouveau_nom .= $suffixe . ".". $extension;

        return $nouveau_nom;
    }

    public function set_thumbnail_logo( $nom ) {

        $this->set_thumbnail( "agence", $nom, $this->width_logo, $this->height_logo );

    }

    public function set_thumbnail_sejour( $nom ) {

        $this->set_thumbnail( "sejour", $nom, $this->width_accueil, $this->height_accueil );
    }

    public function set_thumbnail_galerie( $nom ) {

        $this->set_thumbnail( "galeriethumb", $nom, $this->width_galerie_thumb, $this->height_galerie_thumb );

        $this->set_thumbnail( "galerie", $nom, $this->width_galerie, $this->height_galerie );
        
    }

    public function set_thumbnail ($type, $nom, $width, $height ) {
        $supprimerOriginal = true;
        $dirPath = $this->getRootPath();

        $imagePath   = $dirPath . $nom;
        $thumb       = $this->thumbnailer->create($imagePath, $options = array(), $plugins = array());

        if( $type == "sejour") {
            $thumb->adaptiveResize($width, $height);
            $nouveau_nom = $this->retourne_nom_image_min ( $nom, "-main" );

        } else if( $type == "agence" ) {
            $thumb->resize($width, $height);
            $nouveau_nom = $this->retourne_nom_image_min ( $nom );

        } else if( $type == "galerie" ) {
            $thumb->adaptiveResize($width, $height);
            $nouveau_nom = $this->retourne_nom_image_min ( $nom );

        } else if( $type == "galeriethumb" ) {
            $thumb->adaptiveResize($width, $height);
            $nouveau_nom = $this->retourne_nom_image_min ( $nom, "-thumb" );
            $supprimerOriginal = false;
        }

        $thumb->save( $dirPath . $nouveau_nom);

        // suppression de l'original après avoir généré la miniature
        if( $supprimerOriginal == true ) {
            $this->supprimeImageFileSystem( $nom );
        }
    }

}